package com.ioc.business.order.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ioc.business.order.domain.AggregativeIndicator;
import com.ioc.business.order.domain.bo.aggregative.indicator.AggregativeIndicatorBo;
import com.ioc.business.order.domain.bo.aggregative.indicator.AggregativeIndicatorQueryBo;
import com.ioc.business.order.mapper.AggregativeIndicatorMapper;
import com.ioc.business.order.service.IAggregativeIndicatorService;
import com.ioc.business.order.vo.aggregative.indicator.AggregativeIndicatorImportVo;
import com.ioc.business.order.vo.aggregative.indicator.AggregativeIndicatorPageVo;
import com.ioc.business.order.vo.aggregative.indicator.AggregativeIndicatorVo;
import com.ioc.common.constant.UserConstants;
import com.ioc.common.core.page.TableVo;
import com.ioc.common.exception.ServiceException;
import com.ioc.common.utils.SecurityUtils;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 综合指标表 服务实现类
 * </p>
 *
 * @author WeiZhongRong
 * @since 2025-01-07
 */
@Service
@AllArgsConstructor
public class AggregativeIndicatorServiceImpl extends ServiceImpl<AggregativeIndicatorMapper, AggregativeIndicator> implements IAggregativeIndicatorService {

    @Override
    public TableVo<AggregativeIndicator, AggregativeIndicatorPageVo> selectAggregativeIndicatorPageList(AggregativeIndicatorQueryBo queryBo) {
        List<AggregativeIndicator> result = baseMapper
                .queryPageList(queryBo);
        List<AggregativeIndicatorPageVo> pageVos = new ArrayList<>(result.size());
        if (CollectionUtil.isNotEmpty(result)) {
            pageVos = buildPageVos(result);
        }
        return new TableVo<>(result, pageVos);
    }

    private List<AggregativeIndicatorPageVo> buildPageVos(List<AggregativeIndicator> records) {
        return records.stream().map(record -> {
            AggregativeIndicatorPageVo vo = new AggregativeIndicatorPageVo();
            BeanUtils.copyProperties(record, vo);
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public AggregativeIndicatorVo selectAggregativeIndicatorById(Long aggregativeIndicatorId) {
        AggregativeIndicator aggregativeIndicator = getById(aggregativeIndicatorId);
        if (ObjectUtil.isNotEmpty(aggregativeIndicator)) {
            AggregativeIndicatorVo aggregativeIndicatorVo = new AggregativeIndicatorVo();
            BeanUtils.copyProperties(aggregativeIndicator, aggregativeIndicatorVo);
            return aggregativeIndicatorVo;
        }
        return null;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addAggregativeIndicator(AggregativeIndicatorBo bo) {
        AggregativeIndicator aggregativeIndicator = new AggregativeIndicator();
        BeanUtils.copyProperties(bo, aggregativeIndicator);
        aggregativeIndicator.setCreateBy(SecurityUtils.getUsername());
        aggregativeIndicator.setCreateTime(new Date());
        aggregativeIndicator.setUpdateBy(SecurityUtils.getUsername());
        aggregativeIndicator.setUpdateTime(aggregativeIndicator.getCreateTime());
        aggregativeIndicator.setDelFlag(UserConstants.DEL_FLAG_NORMAL);
        return save(aggregativeIndicator);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateAggregativeIndicator(AggregativeIndicatorBo bo) {
        AggregativeIndicator aggregativeIndicator = new AggregativeIndicator();
        BeanUtils.copyProperties(bo, aggregativeIndicator);
        aggregativeIndicator.setUpdateBy(SecurityUtils.getUsername());
        aggregativeIndicator.setUpdateTime(aggregativeIndicator.getCreateTime());
        return updateById(aggregativeIndicator);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean removeAggregativeIndicator(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return false;
        }
        List<AggregativeIndicator> updateList = idList.stream().map(id -> {
            AggregativeIndicator aggregativeIndicator = new AggregativeIndicator();
            aggregativeIndicator.setAggregativeIndicatorId(id);
            aggregativeIndicator.setDelFlag(UserConstants.DEL_FLAG_REMOVED);
            return aggregativeIndicator;
        }).collect(Collectors.toList());
        return updateBatchById(updateList);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void importAggregativeIndicator(List<AggregativeIndicatorImportVo> voList) {
        List<AggregativeIndicator> aggregativeIndicatorList = verifyImportData(voList);
        saveBatch(aggregativeIndicatorList);
    }

    private List<AggregativeIndicator> verifyImportData(List<AggregativeIndicatorImportVo> voList) {
        if (CollectionUtil.isEmpty(voList)) {
            throw new ServiceException("导入数据不能为空");
        }
        List<AggregativeIndicator> aggregativeIndicatorList = new ArrayList<>(voList.size());
        String username = SecurityUtils.getUsername();
        Date date = new Date();
        for (AggregativeIndicatorImportVo aggregativeIndicatorImportVo : voList) {
            AggregativeIndicator aggregativeIndicator = new AggregativeIndicator();
            BeanUtils.copyProperties(aggregativeIndicatorImportVo, aggregativeIndicator);
            aggregativeIndicator.setCreateBy(username);
            aggregativeIndicator.setCreateTime(date);
            aggregativeIndicator.setUpdateBy(username);
            aggregativeIndicator.setUpdateTime(date);
            aggregativeIndicator.setDelFlag(UserConstants.DEL_FLAG_NORMAL);
            aggregativeIndicatorList.add(aggregativeIndicator);
        }
        return aggregativeIndicatorList;
    }
}
